//单号生成管理
function BasicNumGenerate_main() {
    return {
        dClick: function (cmp, r) {
            BasicNumGenerate_form().view({
                entityId: r.get('id')
            });
        },
        id: 'BasicNumGenerate_main',
        xtype: 'jgrid',
        clazz: BasicNumGenerate,
        order: [],
        where: [],
        top: [{
            name: "numKey",
            xtype: 'textfield',
            op: "like"
        }, {
            name: "currentCode",
            xtype: 'textfield',
            op: 'like'
        }, {
            name: "prefixCode",
            xtype: 'textfield',
            op: 'like'
        }, {
            name: "suffixCode",
            xtype: 'textfield',
            op: 'like'
        }, $.search, '->', {
            key: 'A',
            iconCls: ion.add,
            text: '添加',
            handler: function () {
                BasicNumGenerate_form().save({
                    grid: ['BasicNumGenerate_main']
                });
            }
        }, {
            key: 'D',
            iconCls: ion.del,
            text: '删除',
            handler: function () {
                $('BasicNumGenerate_main').deletes({
                    ptext: $.color('red',
                        '删除后使用该单号生成器的地方会出现BUG。确实删除吗？'),
                    validate: []
                });
            }
        }],
        columns: [
            {
                width: 150,
                "name": "numKey"
            },
            {
                text: '当前单号',
                width: 130,
                "name": "currentCode"
            },
            {
                text: '单号前缀',
                width: 140,
                "name": "prefixCode"
            },
            {
                width: 140,
                text: '单号后缀',
                "name": "suffixCode"
            },
            {
                text: '位数',
                width: 80,
                "name": "digit"
            },
            {
                text: '说明',
                "name": "numCreateReson",
                flex: 1
            }
        ]
    }
}

function BasicNumGenerate_form() {
    var previewEvent = function (cmp, nowValue) {
        //获取form,以及预览单号
        var form = cmp.up("jform"), previewCode = form.get("currentCode");
        var prefixCode = form.get("prefixCode").v(), suffixCode = form.get("suffixCode").v(),
            digit = form.get("digit").v();
        if (form.isValid()) {
            previewCode.setValue(prefixCode + "0".times(digit) + suffixCode);
        }
    };

    return $({
        xtype: 'jform',
        clazz: BasicNumGenerate,
        fieldsSize: 1,
        hides: [],
        width: 450,
        defaults: {
            lw: 90
        },
        validate: [['numKey = ?', 'numKey', '已经存在的键']],
        fields: [
            {
                req: true,
                "name": "numKey",
                listeners: {
                    change: previewEvent
                }
            },
            {
                req: true,
                xtype: "numberfield",
                "name": "digit",
                listeners: {
                    change: previewEvent
                },
                minValue: 6,
                maxValue: 18
            }, {
                "name": "prefixCode",
                listeners: {
                    change: previewEvent
                }
            },
            {
                "name": "suffixCode",
                listeners: {
                    change: previewEvent
                }
            }, {
                "name": "numCreateReson",
                xtype: "textarea"
            },
            {
                text: '单号预览',
                "name": "currentCode",
                readOnly: true
            }
        ]
    })
}